package net.tv90.bfmccore.battlepack;

import lombok.Data;
import net.tv90.bfmccore.util.Cooldown;
import net.tv90.bfmccore.util.HUD.HUD;
import net.tv90.bfmccore.util.HUD.HUDDelayedQueue;
import net.tv90.bfmccore.util.HUD.Text;
import net.tv90.bfmccore.util.vector.Vector2i;
import org.bukkit.entity.Player;

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.tv90.bfmccore.BFMCCore.PLUGIN_ROOT;

@Data
public class Battlepack implements Serializable {

    private static final long serialVersionUID = 12947102640122L;

    /*** 储存玩家获得的战斗包数量 */
    public static Map<String, List<Battlepack>> playerStoredBattlepack;

    static {
        initBattlepackData();
    }

    private EnumBattlepackLevel level;
    private int num;
    private String name;

    public Battlepack(EnumBattlepackLevel level) {
        this.level = level;
        switch (level) {
            case GOLD: {
                num = 5;
                name = "黄金战斗包";
                break;
            }
            case SILVER: {
                num = 3;
                name = "白银战斗包";
                break;
            }
            case BRONZE: {
                num = 1;
                name = "青铜战斗包";
                break;
            }
            case PREMIUM: {
                num = 10;
                name = "高级战斗包";
                break;
            }
            case WEAPON: {
                num = 3;
                name = "武器战斗包";
                break;
            }

        }
    }

    /**
     * 玩家打开所储存的战斗包
     *
     * @param p 玩家
     */
    public static void openBattlepack(Player p) {

        List<Battlepack> battlepackList = playerStoredBattlepack.getOrDefault(p.getName(), new ArrayList<>());

        if (battlepackList == null || battlepackList.isEmpty()) {
            p.sendMessage("§e[管理员]你没有未打开的战斗包!");
            return;
        }

        if (Cooldown.isCool("bp_open", p)) {
            p.sendMessage("§e[管理员]打开战斗包功能处于冷却中，剩余" + Cooldown.getTime("bp_open", p) + "秒");
            return;
        }

        try {
            Battlepack battlepack = battlepackList.get(0);
            battlepackList.remove(battlepack);
            p.sendMessage("§e[管理员]你打开了" + battlepack.getName() + "，获得" + battlepack.getNum() + "物品");

            for (int i = 0; i < battlepack.getNum(); i++) {
                BattlepackItemUtil.getSingleBattlepackItem(p);
            }

            playerStoredBattlepack.put(p.getName(), battlepackList);
            saveBattlepackData();
            new Cooldown("bp_open", 3, p).startForSinglePlayer();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 玩家获得一个战斗包
     *
     * @param p                   玩家
     * @param enumBattlepackLevel 战斗包等级
     */
    public static void getBattlepack(Player p, EnumBattlepackLevel enumBattlepackLevel) {
        List<Battlepack> battlepackList = playerStoredBattlepack.getOrDefault(p.getName(), new ArrayList<>());

        int battlepackNum = battlepackList.size();
        battlepackNum++;
        Battlepack battlepack = new Battlepack(enumBattlepackLevel);
        battlepack.showHUD(p, "custom.Unlock_Battlepack");
        p.sendMessage("§e[管理员]你获得了一个" + battlepack.getName() + ",当前战斗包数量为" + battlepackNum);

        battlepackList.add(battlepack);
        playerStoredBattlepack.put(p.getName(), battlepackList);
        saveBattlepackData();
    }

    /*** 从文件读取玩家已经获得的战斗包物品 */
    public static Map<?, ?> readBattlepackData() {
        File file = new File(PLUGIN_ROOT, "/Battlepack/playerStoredBattlepack.dat");
        try (
                InputStream inputStream = Files.newInputStream(file.toPath());
                ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)
        ) {
            Object object = objectInputStream.readObject();
            System.out.println("读取玩家储存战斗包数据成功");
            return (object == null) ? new HashMap<>() : (Map<?, ?>) object;
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.err.println("Failed to Read Player Stored Battlepack Data!");
        return new HashMap<>();
    }

    /*** 初始化战斗包数据 */
    @SuppressWarnings("unchecked")
    public static void initBattlepackData() {
        try {
            playerStoredBattlepack = (Map<String, List<Battlepack>>) readBattlepackData();
            System.out.println("初始化玩家所储存的战斗包数据成功");
            //System.out.println(playerStoredBattlepack);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        playerStoredBattlepack = new HashMap<>();
    }

    /*** 将玩家已经获得的战斗包物品序列化到文件 */
    public static void saveBattlepackData() {
        File file = new File(PLUGIN_ROOT, "/Battlepack/playerStoredBattlepack.dat");
        try (
                OutputStream outputStream = Files.newOutputStream(file.toPath());
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)
        ) {
            objectOutputStream.writeObject(playerStoredBattlepack);
            System.out.println("序列化玩家所储存战斗包数据成功");
            //System.out.println(playerStoredBattlepack);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 给玩家展示战斗包物品HUD
     *
     * @param p 玩家
     */
    public static void showItemHUD(Player p, String item, String sound) {
        Text text = new Text(new Vector2i(-1, 135), item, 1.0D);
        List<Text> list = new ArrayList<>();
        list.add(text);
        HUD hud = new HUD(p, "[local]KillInfo/attachment/" + item + ".png", "custom.award", list,
                new Vector2i(-1, 20), new Vector2i(110, 110), 2);
        HUDDelayedQueue.addHUD(p, hud);
    }

    /**
     * 给玩家展示战斗包物品HUD
     *
     * @param p 玩家
     */
    public static void showWeaponHUD(Player p, String item, String sound) {
        Text text = new Text(new Vector2i(-1, 135), item, 1.0D);
        List<Text> list = new ArrayList<>();
        list.add(text);
        HUD hud = new HUD(p, "[local]KillInfo/weapon/" + item + ".png", "custom.award", list,
                new Vector2i(-1, 20), new Vector2i(110, 110), 2);
        HUDDelayedQueue.addHUD(p, hud);
    }

    /**
     * 给玩家展示战斗包HUD
     *
     * @param p 玩家
     */
    public void showHUD(Player p, String sound) {
        Text text = new Text(new Vector2i(-1, 135), name, 1.0D);
        List<Text> list = new ArrayList<>();
        list.add(text);
        HUD hud = new HUD(p, "[local]Battlepack/" + level.name() + ".png", "custom.award", list,
                new Vector2i(-1, 20), new Vector2i(110, 110), 5);
        HUDDelayedQueue.addHUD(p, hud);
    }

    @Override
    public String toString() {
        return name;
    }
}
